package com.yzt.config.security;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;

import com.yzt.base.dto.MenuDto;
import com.yzt.base.entity.BaseUser;
import com.yzt.sys.entity.Permission;
import com.yzt.sys.entity.Role;
import com.yzt.sys.service.IMenuService;
import com.yzt.sys.service.IPermissionService;
import com.yzt.sys.service.IRoleMenuService;
import com.yzt.sys.service.IRoleService;
import com.yzt.sys.service.IUserRoleService;

@Configuration
public class UserLoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
	@Resource
	private IMenuService menuService;
	@Autowired
	private IRoleMenuService roleMenuService;
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IUserRoleService userRoleService;
	@Autowired
	private IPermissionService permissionService;

	@Override
	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {
		super.onAuthenticationSuccess(request, response, authentication);

		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		Object principal = auth.getPrincipal();
		if (principal instanceof BaseUser) {
			BaseUser user = (BaseUser) principal;

			List<GrantedAuthority> list = new ArrayList<>();

			List<Long> roleIds = userRoleService.getRoleIds(user.getId());

			List<Role> roleList = roleService.roleList(roleIds);// 获取所有角色信息列表
			for (Role role : roleList) {
				SimpleGrantedAuthority sauth = new SimpleGrantedAuthority("ROLE_" + role.getKeyword());
				list.add(sauth);
			}
			List<Permission> permissionList = permissionService.getPermissions(roleIds);
			for (Permission p : permissionList) {
				SimpleGrantedAuthority sauth = new SimpleGrantedAuthority(p.getKeyword());
				list.add(sauth);
			}

			List<Long> menuIds = roleMenuService.getMenuIdList(roleIds);// 获取所有拥有的菜单ID
			List<MenuDto> menuDtos = menuService.getMenuDtoList(menuIds); // 获取所有菜单信息

			user.setMenuList(menuDtos);

			UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user,
					auth.getCredentials(), list);
			SecurityContextHolder.getContext().setAuthentication(token);
		}

	}
}
